package com.ly.user_system.retry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetrySynchronizationManager;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import java.util.function.Function;

@Component
public class RetryTemplateUtil {


    private static RetryTemplate retryTemplate;

    @Autowired
    public static void setRetryTemplate(RetryTemplate retryTemplate) {
        RetryTemplateUtil.retryTemplate = retryTemplate;
    }

    public static<T> T retry(Function<RetryContext,T> function){
        return retryTemplate.execute(function::apply);
    }

    public static<T> T retryWithRecover(Function<RetryContext,T> function,Function<RetryContext,T> recover){

        return retryTemplate.execute(function::apply,recover::apply);
    }




}
